"""
__author__ = '霍格沃兹测试开发学社'
__desc__ = '更多测试开发技术探讨，请访问：https://ceshiren.com/t/topic/15860'
"""

## 基本的方法，driver, find ，click,swipe。。。
import time
from appium.webdriver.common.appiumby import AppiumBy
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.webdriver import WebDriver
from selenium.common.exceptions import NoSuchElementException
from base.black_handle import black_wrapper


class BasePage:
    impicit_wait = 15
    black_list = [(AppiumBy.ID, "com.xueqiu.android:id/iv_close"),
                  (AppiumBy.ID, "com.xueqiu.android:id/iv_action_back"),
                  (AppiumBy.XPATH, "//*[@text='确定']")
                  ]

    def __init__(self, driver: WebDriver = None):
        self.driver = driver

    @black_wrapper
    def find(self, by, value=None):
        if value:
            return self.driver.find_element(by, value)
        else:
            return self.driver.find_element(*by)

    def finds(self, by, value=None):
        if value:
            return self.driver.find_element(by, value)
        else:
            return self.driver.find_element(*by)

    # def find(self, by, locator):
    #     try:
    #         return self.driver.find_element(by, locator)
    #     except Exception as e:
    #         print("未找到")
    #         for e in self.black_list:
    #             print(f"遍历黑名单：{e}")
    #             eles = self.driver.find_elements(*e)
    #             # sleep(1)
    #             if len(eles) >0:
    #                 eles[0].click()
    #                 return self.find(by,locator)
    #         raise e
    # def find(self, by, locator):
    #     try:
    #         return self.driver.find_element(by, locator)
    #     except Exception as e:
    #
    #         for black in self.black_list:
    #             eles = self.driver.find_elements(*black)
    #             if len(eles) > 0:
    #                 eles[0].click()
    #                 return self.find(by, locator)
    #         raise e

    def find_and_click(self, by, value=None):
        self.find(by, value).click()

    def find_and_send(self,text, by, value=None):
        self.find(by, value).send_keys(text)

    def find_and_gettext(self, by, value=None):
        return self.find(by, value).text

    def screenshot(self, name):
        # 截图
        self.driver.save_screenshot(name)

    def get_time(self):
        t = time.localtime(time.time())
        cur_time = time.strftime("%Y-%m-%d_%H_%M_%S", t)
        print(f"当前时间为：{cur_time}")
        return cur_time

    def get_pagesource(self):
        return self.driver.page_source


    def swipe_find(self, text, num=3):
        # 循环三次查找
        self.driver.implicitly_wait(1)
        for i in range(num):
            try:
                ele = self.driver.find_element(MobileBy.XPATH, f"//*[@text='{text}']")
                self.driver.implicitly_wait(self.impicit_wait)
                return ele
            except:
                # 滑动操作
                # 获取屏幕的尺寸 'width', 'height'
                size = self.driver.get_window_size()
                # 屏幕宽
                width = size.get("width")
                # 屏幕高
                height = size.get("height")
                # 起点x
                start_x = width / 2
                # 起点y  屏幕的y*0.8
                start_y = height * 0.8
                # 终点x
                end_x = start_x
                # 终点y
                end_y = height * 0.3
                duration = 2000
                self.driver.swipe(start_x, start_y, end_x, end_y, duration)

            if i == num - 1:
                self.driver.implicitly_wait(self.impicit_wait)
                raise NoSuchElementException(f"找了{num}次，未找到")

    def getattr(self, by, locator=None, attribute="text"):
        return self.find(by, locator).get_attribute(attribute)

    def get_toast(self):
        # self.find(AppiumBy.XPATH, f"//*[@class='{text}']")
        result = self.driver.find_element(MobileBy.XPATH,
                                          "//*[@class='android.widget.Toast']").get_attribute("text")
        return result


